package com.example.restatr.Atcivity.DataAnalysis;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.restatr.R;
import com.example.restatr.Tools;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.data.RadarData;
import com.github.mikephil.charting.data.RadarDataSet;
import com.github.mikephil.charting.data.RadarEntry;
import com.github.mikephil.charting.formatter.ValueFormatter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class DataAnalysisAt extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private Spinner spinner;
    private TextView chartDescribed;
    private PieChart pieChart;
    private BarChart barChart;
    private RadarChart radarChart;
    private List<PieEntry> pieEntryList = new ArrayList<>();
    private List<Integer> pieColor = new ArrayList<>();
    private List<BarEntry> barEntryList = new ArrayList<>();
    private List<Integer> barColor = new ArrayList<>();
    private List<RadarEntry> radarEntryList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_analysis_at);

        toolbar = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        spinner = findViewById(R.id.spinner);
        chartDescribed = findViewById(R.id.chartDescribed);
        pieChart = findViewById(R.id.pieChart);
        barChart = findViewById(R.id.barChart);
        radarChart = findViewById(R.id.radarChart);

        Tools.SetBarTitle(toolbar, barTitle, "数据分析", search, this, false);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        pieChart.setVisibility(View.VISIBLE);
                        barChart.setVisibility(View.GONE);
                        radarChart.setVisibility(View.GONE);
                        pieEntryList.clear();
                        pieColor.clear();
                        pieChart.getLegend().setTextSize(16);
                        pieChart.getDescription().setEnabled(false);
                        pieChart.setEntryLabelColor(Color.parseColor("#000000"));
                        pieChart.setDrawHoleEnabled(false);//设置是否是圆环，默认为true
                        chartDescribed.setText("新冠感染人数比例");
                        Collections.addAll(pieEntryList, new PieEntry(70, "感染人数"), new PieEntry(30, "未感染人数"));
                        Collections.addAll(pieColor, Color.parseColor("#F17548"), Color.parseColor("#00FF00"));
                        PieDataSet pieDataSet = new PieDataSet(pieEntryList, "");
                        pieDataSet.setSliceSpace(5f); //设置饼状Item之间的间隙
                        pieDataSet.setValueTextSize(16);
                        pieDataSet.setColors(pieColor);
                        PieData pieData = new PieData(pieDataSet);
                        pieChart.setData(pieData);
                        pieChart.invalidate();
                        break;
                    case 1:
                        break;
                    case 2:
                        barChart.setVisibility(View.VISIBLE);
                        pieChart.setVisibility(View.GONE);
                        radarChart.setVisibility(View.GONE);
                        barEntryList.clear();
                        barColor.clear();
                        barChart.getLegend().setEnabled(false);
                        barChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
                        barChart.getXAxis().setValueFormatter(new ValueFormatter() {
                            @Override
                            public String getFormattedValue(float value) {
                                String name = "";
                                if (value == 0) {
                                    name = "蓝莓";
                                }
                                if (value == 1) {
                                    name = "梨子";
                                }
                                if (value == 2) {
                                    name = "黄桃";
                                }
                                if (value == 3) {
                                    name = "山竹";
                                }
                                return name;
                            }
                        });
                        barChart.getXAxis().setLabelCount(4,false);
                        barChart.getXAxis().setTextSize(16);
                        barChart.getDescription().setEnabled(false);
                        chartDescribed.setText("各产品销售数量");
                        Collections.addAll(barEntryList, new BarEntry(0, 5, "蓝莓"), new BarEntry(1, 7, "梨子"), new BarEntry(2, 3, "黄桃"), new BarEntry(3, 9, "山竹"));
                        Collections.addAll(barColor, Color.parseColor("#0271F7"), Color.parseColor("#12EE90"), Color.parseColor("#FFCD00"),
                                Color.parseColor("#FFFAE5"));
                        BarDataSet barDataSet = new BarDataSet(barEntryList, "");
                        barDataSet.setValueTextSize(16);
                        barDataSet.setColors(barColor);
                        BarData barData = new BarData(barDataSet);
                        barChart.setData(barData);
                        break;
                    case 3:
                        radarChart.setVisibility(View.VISIBLE);
                        barChart.setVisibility(View.GONE);
                        pieChart.setVisibility(View.GONE);
                        radarChart.getDescription().setEnabled(false);
                        chartDescribed.setText("能力属性分布图");
                        radarChart.getXAxis().setTextSize(16);
                        radarChart.getLegend().setEnabled(false);
                        Collections.addAll(radarEntryList, new RadarEntry(11), new RadarEntry(34), new RadarEntry(45)
                                , new RadarEntry(21), new RadarEntry(23));
                        radarChart.getXAxis().setValueFormatter(new ValueFormatter() {
                            @Override
                            public String getFormattedValue(float value) {
                                String[] AbilityToType = new String[]{"智力", "颜值", "武力", "敏捷", "社交"};
                                return AbilityToType[(int) value];
                            }
                        });
                        RadarDataSet radarDataSet = new RadarDataSet(radarEntryList, "");
                        radarDataSet.setValueTextSize(16);
                        radarDataSet.setColor(Color.parseColor("#0B1416"));
                        RadarData radarData = new RadarData(radarDataSet);
                        radarChart.setData(radarData);
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


    }
}